import { defineStore } from 'pinia'
import { type RouteRecordRaw, useRouter } from 'vue-router'
import type { Menu } from '@/modules/admin/manager/entity'

interface MenuMap {
    [route: string]: Menu;  // 添加字符串索引签名
}
declare module 'vue-router' {
    interface RouteMeta {
        order?: number;
        name?: string;
        default?: boolean;
    }
}

export const useRouterStore = defineStore('router', () => {
    const routerAll = useRouter()
    // 递归拿到所有当前用户的具有菜单或者是不需要验证的路由(route.name)
     function filterRouterByName(routes: RouteRecordRaw[], menuMap: MenuMap) {
        routes = routes.filter(route => {
            return route && (
              (route.name && menuMap[route.name as string]) ||  route.meta?.default
            )
        })
        if (routes) {
            routes = routes.map(route => {
                if (route.meta && menuMap[route.name as string]) {
                    route.meta.name = menuMap[route.name as string].name
                    route.meta.order = menuMap[route.name as string].order
                }
                if (route.children && route.children.length > 0) {
                    route.children = filterRouterByName(route.children, menuMap)
                }
                return route
            })
            // 按order排序
            routes.sort((a, b) => {
                if(a.meta?.order && b.meta?.order){
                    return a.meta.order - b.meta.order
                }else{
                    return 0
                }
            })

        }
        return routes
    }

    function getRoutesByCurrentUser(menus:Menu[],routes: RouteRecordRaw[]) {
        const menuMap = {} as MenuMap
        if (menus && menus.length > 0) {
            // 设置 menuMap
            menus.forEach(menu =>
              menuMap[menu.route] = menu
            )
            return filterRouterByName(routes, menuMap);
        } else {
            return []
        }
    }
    // 获取当前用户的菜单
    function getCurrentUserRouterList(menus:Menu[],type:string) {
        let typeRouterAll
        // 根据分类获取系统的菜单
        if(type === 'portal'){
            typeRouterAll = routerAll.getRoutes()
                .filter(route => route.name === 'portal')[0]
                .children
                .filter(route => route.name !== 'portalHome' && route.name && route.meta?.name) || []
        }else{
            typeRouterAll = routerAll.getRoutes()
                .filter(route => route.name === 'admin')[0]
                .children
                .filter(route => route.name !== 'adminHome' && route.name && route.meta?.name) || []
        }
        return getRoutesByCurrentUser(menus,typeRouterAll)
    }

    function getCurrentUserRouterRoot(menus:Menu[],type:string) {
        let typeRouter
        // 根据分类获取系统的菜单
        if(type === 'portal'){
            typeRouter = routerAll.getRoutes()
              .filter(route => route.name === 'portal')
        }else{
            typeRouter = routerAll.getRoutes()
              .filter(route => route.name === 'admin')
        }
        return getRoutesByCurrentUser(menus,typeRouter)
    }
    return { routerAll, getCurrentUserRouterList,getCurrentUserRouterRoot}
})